import io

from fontTools.ttLib import TTFont

from PIL import Image , ImageFont,ImageDraw

from  ddddocr import DdddOcr

def get_font_maps(path):
    # 指定解析字体文件
    font = TTFont(path)

    # 指定绘制的字体文件
    img_font = ImageFont.truetype(path, 10)

    font_map = {}

    for code, name in font.getBestCmap().items():
        if code == 120:
            continue

        # 创建图片
        img = Image.new("RGB", (32, 32), (255, 255, 255))


        # 绘制图片
        draw = ImageDraw.Draw(img)
        draw.text((0, 0), chr(code), font=img_font, fill=(0, 0, 0))

        # 保存 图片
        # img.save(f"./static/font/img/{code}.png")
        img_bytes = io.BytesIO()
        img.save(img_bytes, "JPEG")
        # print(img_bytes.getvalue())

        ocr = DdddOcr(show_ad=False)

        r = ocr.classification(img_bytes.getvalue())

        font_map[hex(code)] = r
    return font_map


if __name__ == '__main__':
    path = "../static/font/file"
    print(get_font_maps(path))













